package gold.digger;

import gold.utils.InputUtil;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LCJZOF119 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int longestConsecutive(int[] nums) {
            Map<Integer, Integer> mp = new HashMap<>();
            for (int num : nums) {
                mp.put(num, 1);
            }

            int ans = null == nums || nums.length <= 0 ? 0 : 1;
            for (int num : nums) {
                if (mp.get(num) > 1) {
                    ans = Math.max(ans, mp.get(num));
                } else {
                    int curSeqCnt = 1, cur = num - 1;
                    while (mp.containsKey(cur) && mp.get(cur) == 1) {
                        cur--;
                        curSeqCnt++;
                    }

                    int curLen = curSeqCnt + mp.getOrDefault(cur, 0);
                    ans = Math.max(ans, curLen);
                    cur = num;
                    for (int i = curLen; mp.containsKey(cur) && i >= 1; i--) {
                        mp.put(cur--, i);
                    }
                }
            }

            return ans;
        }
    }

    public void run() {
        System.out.println(new Solution().longestConsecutive(InputUtil.toIntegerArray("[100,4,200,1,3,2]")));
        System.out.println(new Solution().longestConsecutive(InputUtil.toIntegerArray("[0,3,7,2,5,8,4,6,0,1]")));
        System.out.println(new Solution().longestConsecutive(new int[]{}));
    }

    public static void main(String[] args) throws Exception {
        LCJZOF119 an = new LCJZOF119();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
